package com.example.utils;

import com.example.pojo.sandbox.ExecuteCodeResponse;
import com.example.pojo.sandbox.ExecuteMessage;
import com.example.pojo.judge.JudgeInfo;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ResponseUtils {

    public ExecuteCodeResponse getOutputResponse(List<ExecuteMessage> executeMessages) {
        // 创建返回类封装最终返回信息
        ExecuteCodeResponse executeCodeResponse = new ExecuteCodeResponse();
        List<ExecuteMessage> executeMessageList = new ArrayList<>();
        for (ExecuteMessage message : executeMessages) {
            // 存在错误信息时将错误信息写入并设置状态码
            if (message.getErrorMessage() != null) {
                executeCodeResponse.setMessage(message.getErrorMessage());
                executeCodeResponse.setStatus(2);
                break;
            }
            executeMessageList.add(message);
        }
        executeCodeResponse.setExecuteMessageList(executeMessageList);
        // 正常运行完成设置状态码
        if (executeMessageList.size() == executeMessages.size()) {
            executeCodeResponse.setStatus(0);
        }
        return executeCodeResponse;
    }


}
